package com.smalls.newvideotwo.utils;

import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.WindowManager;
import android.widget.TextView;

import java.lang.reflect.Method;

/**
 * 
 * @author lipan
 */
public class MeasureUtil {
	private static int screenHeight = 0;//屏幕高度
	private static int screenWidth = 0;//屏幕宽度
	private static float density = 1.0f;
	private static float scaleDensity = 1.0f;
	private static boolean init = false;
	private static float frontSize;
	public static float getFrontSize() {
		return frontSize;
	}

	private static float touchHeight;
	public static float getTouchHeight() {
		return touchHeight;
	}

	public static float getTouchWidth() {
		return touchWidth;
	}

	private static float touchWidth;
	private MeasureUtil() {
		throw new SecurityException("Are not allowed to create an instance!");
	}

	/**
	 * Before using the methods of this class,call the method<br>
	 * The following variable assignment:<br>
	 * {@link MeasureUtil#screenWidth}<br>
	 * {@link MeasureUtil#screenHeight}<br>
	 * {@link MeasureUtil#density}<br>
	 * {@link MeasureUtil#scaleDensity}<br>
	 * 
	 * @param context	public static KantvStoreApplication getInstance() {
		return netCube;
	}

	 */
	public static final void init(Context context) {
		if (init) {
			return;
		}
		DisplayMetrics metrics = new DisplayMetrics();
		WindowManager manager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		display.getMetrics(metrics);
		screenWidth = metrics.widthPixels;
		int ver = Build.VERSION.SDK_INT;
		if (ver == SystemBuild.VERSION_CODES.HONEYCOMB_MR2) {
			try {
				Method method = display.getClass().getMethod("getRealHeight");
				screenHeight = (Integer) method.invoke(display);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (ver > SystemBuild.VERSION_CODES.HONEYCOMB_MR2
				&& ver < SystemBuild.VERSION_CODES.JELLY_BEAN_MR1) {
			try {
				Method method = display.getClass().getMethod("getRawHeight");
				screenHeight = (Integer) method.invoke(display);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (ver >= SystemBuild.VERSION_CODES.JELLY_BEAN_MR1) {
			try {
				Method method = display.getClass().getMethod("getRealMetrics",
						DisplayMetrics.class);
				method.invoke(display, metrics);
				screenHeight = metrics.heightPixels;
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			screenHeight = metrics.heightPixels;
		}
		density = metrics.density;
		scaleDensity = metrics.scaledDensity;
		init = true;
		frontSize=screenWidth/metrics.density/1280;
		touchHeight=screenHeight/6;
		touchWidth=screenWidth/6;
	}

	/**
	 * The dp value into the px value
	 * 
	 * @param dpValue
	 *            dp
	 * @return (int)px value
	 * @exception ExceptionInInitializerError
	 */
	public static final float dp2px(float dpValue) {
		checkInit();
		return dpValue * density;
	}

	public static final void setTextSize(TextView text,float dp){
		text.setTextSize(TypedValue.COMPLEX_UNIT_DIP,frontSize*dp);
	}
	
	/**
	 * The px value into the dp
	 * 
	 * @param px
	 * @return (float)dp value
	 * @exception ExceptionInInitializerError
	 */
	public static final float px2dp(float px) {
		checkInit();
		return px / density;
	}

	/**
	 * Will px value converted to sp
	 * 
	 * @param pxValue
	 * @return (float)sp value
	 * @exception ExceptionInInitializerError
	 */
	public static float px2sp(float pxValue) {
		checkInit();
		return pxValue / scaleDensity;
	}

	/**
	 * The sp value converted to the px
	 * 
	 * @param spValue
	 * @return (float)px value
	 * @exception ExceptionInInitializerError
	 */
	public static float sp2px(float spValue) {
		checkInit();
		return spValue * scaleDensity;
	}
	
	/**
	 * The dp value converted to the sp
	 * 
	 * @param dpValue
	 * @return (float)sp value
	 * @exception ExceptionInInitializerError
	 */
	public static float dp2sp(float dpValue){
		checkInit();
		return dpValue*density/scaleDensity;
	}
	
	/**
	 * The sp value converted to the dp
	 * 
	 * @param spValue
	 * @return (float)dp value
	 * @exception ExceptionInInitializerError
	 */
	public static float sp2dp(float spValue){
		checkInit();
		return spValue*scaleDensity/density;
	}

	/**
	 * Ratio of the screen width
	 * 
	 * @return (float)ratio
	 * @exception ExceptionInInitializerError
	 */
	public static final float getWidthRatio() {
		checkInit();
		return  screenWidth/(float) Constant.TEMPLE_SCREEN_WIDTH;
	}

	/**
	 * According to ratio to calculate the size fit on the screen width
	 * 
	 * @param dp
	 *            float
	 * @return px value
	 * @exception ExceptionInInitializerError
	 */
	public static final float getWidthSize(float dp) {
		return getWidthRatio() * dp;
	}
	
	/**
	 * According to ratio to calculate the size fit on the screen width
	 * 
	 * @param context Context
	 * @param dimenId Dimen resource Id
	 * @return px value
	 * @exception ExceptionInInitializerError
	 */
	public static final float getWidthSize(Context context,int dimenId){
		return getWidthSize(context.getResources().getDimensionPixelSize(dimenId));
	}
	
	public static final int getScreenWidth(){
		checkInit();
		return screenWidth;
	}
	
	public static final int getScreenHeight(){
		checkInit();
		return screenHeight;
	}

	/**
	 * Ratio of the screen height
	 * 
	 * @return (float)ratio
	 * @exception ExceptionInInitializerError
	 */
	public static final float getHeightRatio() {
		checkInit();
		return screenHeight/(float)Constant.TEMPLE_SCREEN_HEIGHT;
	}

	/**
	 * According to ratio to calculate the size fit on the screen height
	 * 
	 * @param dp
	 *            float
	 * @return px value
	 * @exception ExceptionInInitializerError
	 */
	public static final float getHeightSize(float dp) {
		return getHeightRatio() * dp;
	}
	
	/**
	 * According to ratio to calculate the size fit on the screen height
	 * 
	 * @param context Context
	 * @param dimenId Dimen resource Id
	 * @return px value
	 * @exception ExceptionInInitializerError
	 */
	public static final float getHeightSize(Context context,int dimenId){
		return getHeightSize(context.getResources().getDimensionPixelSize(dimenId));
	}

	/**
	 * Before using the methods of this class, first check whether has been
	 * calling the method {@link MeasureUtil#init(Context)}
	 * 
	 * @exception ExceptionInInitializerError
	 */
	private static final void checkInit() {
		if (!init) {
			throw new ExceptionInInitializerError(
					"After initialization using the methods of this class.");
		}
	}

}
